﻿package jokers{
	import jokers.*;
	import flash.utils.*;
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;

	//保存的全是全局信息
    public class JAnimation { 
		private static const DELAY:int = 30;
		private static const DELETE_DELAY:int = 500;
		private static const FADE_TIME:int = 20;
		private static const FADE_DES:int = 3;
		private static const MOVE_TIME:int = 10;
		private static const FALL_TIME:int = 8;
		private static const SHOW_TIME:int = 30;
		private static const SHOW_DES:int = 50;
		private static const TIP_SHOW_TIME:int = 50;
		private static const TIP_HIDE_TIME:int = 30;
	
		private static var id:uint = 0;
		
		public static function initUI(matrix:Array,func:Function = null):void{
			var ja:JAnimation = new JAnimation();
			ja.setFadeMatrix(matrix,func);
			
			id = setInterval(ja.fadeInMatrix,DELAY);
			ja.id = id;
		}
	
		public static function exchangeCrystal(mc1:MovieClip,mc2:MovieClip,func:Function = null):void{
			if(mc1.sta != JCrystal.STATE_NORMAL) return;
			if(mc2.sta != JCrystal.STATE_NORMAL) return;
			
			mc1.sta = JCrystal.STATE_EXCHANGING;
			mc2.sta = JCrystal.STATE_EXCHANGING;
			
			var ja:JAnimation = new JAnimation();
			ja.setExchangeMC(mc1,mc2,func);
			
			id = setInterval(ja.exchangePlay,DELAY);
			ja.id = id;
		}
		
		public static function deleteCrystal(mc:MovieClip,func:Function = null):void{
			if(mc == null){
				return;
			}
				 
			if(mc.sta != JCrystal.STATE_NORMAL) return;
			
			mc.sta = JCrystal.STATE_DELETE;
			
			var ja:JAnimation = new JAnimation();
			ja.setDeleteMC(mc,func);
			
			ja.id = setInterval(ja.deletePlay,DELETE_DELAY);
		}
		
		public static function fallDownCrystal(mc:MovieClip,func:Function = null):void{
			if(mc == null){
				trace(mc);
				return;
			}
				 
			if(mc.sta != JCrystal.STATE_NORMAL) return;
			
			mc.sta = JCrystal.STATE_FALLDOWN;
			
			var ja:JAnimation = new JAnimation();
			ja.setFallMC(mc,func);
			
			ja.id = setInterval(ja.fallPlay,DELAY);
		}
		
		//显示连击
		public static function showCombo(mc:MovieClip,num:int,func:Function = null):void{
			if(mc == null){
				return;
			}
			
			var ja:JAnimation = new JAnimation();
			ja.setComboInfo(mc,num,func);
			
			ja.id = setInterval(ja.comboPlay,DELAY);
		}
		
		public static function showTip(mc:MovieClip,func:Function = null):void{
			if(mc == null){
				return;
			}
			
			var ja:JAnimation = new JAnimation();
			ja.setTipInfo(mc,func);
			
			ja.id = setInterval(ja.tipPlay,DELAY);
		}
		
		//-------------------------------------------------------
		private var mc1:MovieClip;
		private var mc2:MovieClip;
		
		private var matrix:Array;
		
		private var time:int = 0;
		private var id:uint;
		
		private var desx:Number;
		private var desy:Number;
		
		private var completeFunc:Function;
		
		//-------------------------------------------------------
		public function setId(id:uint):void{
			this.id = id;
		}
		
		//----------------------显示提示
		
		private function setTipInfo(mc:MovieClip,func:Function):void{
			this.mc1 = mc;
			
			completeFunc = func;
			
			this.mc2 = new Tips();
			this.mc2.x = mc1.x;
			this.mc2.y = mc1.y;
			
			if(JPara.mc_tip != null) if(JMatrix.getInstance().topStage.contains(JPara.mc_tip)) JMatrix.getInstance().topStage.removeChild(JPara.mc_tip);
			JPara.mc_tip = this.mc2;
			
			JMatrix.getInstance().topStage.addChild(mc2);
		}
		
		private function tipPlay():void{
			if(this.mc1 == null || this.mc1.sta != JCrystal.STATE_NORMAL){
				if(JMatrix.getInstance().topStage.contains(mc2)) JMatrix.getInstance().topStage.removeChild(mc2);
				JPara.mc_tip = null;
				clearInterval(this.id);
				
				if(completeFunc != null) completeFunc();
				
				delete(this);
			}
			
			return;
			time++;
			//mc2.y -= desy;
			//mc2.alpha = (SHOW_TIME - time) / SHOW_TIME;
			
			if(time >= TIP_SHOW_TIME && time < TIP_SHOW_TIME + TIP_HIDE_TIME){
				mc2.alpha = (TIP_SHOW_TIME + TIP_HIDE_TIME - time) / TIP_HIDE_TIME;
			}else if(time >= TIP_SHOW_TIME + TIP_HIDE_TIME){
				JMatrix.getInstance().topStage.removeChild(mc2);
				clearInterval(this.id);
				
				if(completeFunc != null) completeFunc();
				
				delete(this);
			}
		}
		
		//----------------------显示连击
		
		private function randomColor():ColorTransform{
			var red:Number = Math.random() * 100;
  			var green:Number = Math.random() * 100;
  			var blue:Number = Math.random() * 100;
  			// 使用随机颜色创建并返回 ColorTransform  对象
  			return new ColorTransform(1,1,1,1,red,green,blue,0);
		}
		
		private function setComboInfo(mc:MovieClip,num:int,func:Function):void{
			this.mc1 = mc;
			
			desy = JMatrix.getInstance().des / SHOW_DES;
			
			completeFunc = func;
			
			this.mc2 = new JCombo();
			this.mc2.x = mc1.x;
			this.mc2.y = mc1.y;
			this.mc2.scaleX = JMatrix.getInstance().per*0.6;
			this.mc2.scaleY = JMatrix.getInstance().per*0.6;
			
			this.mc2.transform.colorTransform = this.randomColor();
			
			for(var i:int = 0 ; i < num.toString().length ; i++){
				var tn:Number = Number(num.toString().substr(i,1));
				
				var tmc:MovieClip = new JNumber();
				tmc.gotoAndStop(tn+1);
				tmc.x = 220 + i*60;
				
				this.mc2.addChild(tmc);
			}
			
			this.mc2.x -= this.mc2.width * 0.4;
			
			this.mc2.addChild(tmc);
			
			JMatrix.getInstance().topStage.addChild(mc2);
		}
		
		private function comboPlay():void{
			time++;
			mc2.y -= desy;
			mc2.alpha = (SHOW_TIME - time) / SHOW_TIME;
			
			if(time >= SHOW_TIME){
				JMatrix.getInstance().topStage.removeChild(mc2);
				clearInterval(this.id);
				
				if(completeFunc != null) completeFunc();
				
				delete(this);
			}
		}
		
		//----------------------渐进宝石矩阵
		
		private function setFadeMatrix(matrix:Array,func:Function):void{
			this.matrix = matrix;
			completeFunc = func;
			time = 0;
		}
		
		private function fadeInMatrix():void{
			time++;
			for(var y:int = 0 ; y < JMatrix.getInstance().height ; y++){
				for(var x:int = 0 ; x < JMatrix.getInstance().width ; x++){
					var c:MovieClip = matrix[x][y];
					if(c == null) continue;
					
					c.alpha = (time + ((JMatrix.getInstance().height-y)-JMatrix.getInstance().height) * FADE_DES)/FADE_TIME;
				}
			}
			
			
			if(time == FADE_DES*JMatrix.getInstance().height+FADE_TIME){
				if(completeFunc != null) completeFunc(mc1,mc2);
				clearInterval(this.id);
				delete(this);
			}
		}
		
		//----------------------降落宝石
		
		private function setFallMC(mc:MovieClip,func:Function):void{
			this.mc1 = mc;
			desy = JMatrix.WIDTH / JMatrix.getInstance().width;
			desy = desy / FALL_TIME;
			
			completeFunc = func;
			
			time = 0;
		}
		
		private function fallPlay():void{
			if(JTimer.getInstance().isStop()){
				clearInterval(this.id);
				delete(this);
				return;
			}
			
			mc1.y += desy;
			
			time++;
			if(time == FALL_TIME){
				if(completeFunc != null) {
					if(completeFunc(mc1)){
						time = 0;
					}else{
						mc1.sta = JCrystal.STATE_NORMAL;
						clearInterval(this.id);
						JMatrix.getInstance().checkSys();
						delete(this);
					}
				}else{
					mc1.sta = JCrystal.STATE_NORMAL;
					clearInterval(this.id);
					JMatrix.getInstance().checkSys();
					delete(this);
				}
			}
		}
		
		//----------------------删除宝石
		
		private function setDeleteMC(mc:MovieClip,func:Function):void{
			this.mc1 = mc;
			mc.alpha = 0.1;
			
			completeFunc = func;
			
			var killer:MovieClip = new Killer();
			killer.x = mc.x;
			killer.y = mc.y;
			killer.rotation = int(Math.random()*360);
			killer.scaleX = 0.7 + Math.random()*0.4;
			killer.scaleY = 0.7 + Math.random()*0.4;
			killer.alpha = 0.7 + Math.random()*0.3;
			
			mc2 = killer;
			if(completeFunc != null) completeFunc(mc1);
			
			JMatrix.getInstance().effctStage.addChild(killer);
		}
		
		private function deletePlay():void{
			JMatrix.getInstance().effctStage.removeChild(mc2);
			clearInterval(this.id);
			delete(this);
		}
		
		//----------------------交换宝石
		
		private function setExchangeMC(mc1:MovieClip,mc2:MovieClip,func:Function):void{
			this.mc1 = mc1;
			this.mc2 = mc2;
			
			completeFunc = func;
			
			time = 0;
			
			desx = (mc1.x - mc2.x)/MOVE_TIME;
			desy = (mc1.y - mc2.y)/MOVE_TIME;
		}
		
		private function exchangePlay():void{
			if(JTimer.getInstance().isStop()){
				clearInterval(this.id);
				delete(this);
				return;
			}
			
			mc1.x-= desx;
			mc2.x+= desx;
			
			mc1.y-= desy;
			mc2.y+= desy;
			
			time++;
			if(time == MOVE_TIME){
				mc1.sta = JCrystal.STATE_NORMAL;
				mc2.sta = JCrystal.STATE_NORMAL;
			
				completeFunc(mc1,mc2);
				clearInterval(this.id);
				delete(this);
			}
		}
	}
}